﻿using System;
using System.Collections.Generic;
using System.IO;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Common.Models;
using Module.Core.BusinessLogic.CmsContents;
using Module.Core.BusinessLogic.Products;
using Module.Core.Common;
using Module.Core.EntitiesInfo.TablesInfo;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.Utilities;
namespace Web.Cms.Products
{
    public partial class CreateEditProducts : AdminUserControl
    {
        #region Khai báo
        private int productId = 0;
        private int cityId=0;
       #endregion

        #region PageLoad
        protected void Page_Load(object sender, EventArgs e)
        {         
            try
            {
                if (!Directory.Exists(Server.MapPath("/Uploads/" + CurrentUser.UserID)))
                    Directory.CreateDirectory(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtAvatarSmail.fpUploadDir = txtAvatarSmail.fpUploadDir = "/Uploads/" + CurrentUser.UserID;
                txtAvatarBig.fpUploadDir = txtAvatarBig.fpUploadDir = "/Uploads/" + CurrentUser.UserID;
                txtDescription.Height = 150;               
                txtDescription.Width = 600;

                txtDescription_Note.Height = 150;
                txtDescription_Note.Width = 600;
                txtDescription_Hot.Height = 150;
                txtDescription_Hot.Width = 600;

                txtContents.Height = 300;
                txtContents.Width = 600;
                txtDescription.SetSecurityFilesGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtDescription.SetSecurityFlashGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtDescription.SetSecurityGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtDescription.SetSecurityImageGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtDescription.SetSecurityMediaGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtDescription.SetSecurityTemplateGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                productId = ObjConvertUtility.ToInt32(Request.QueryString["productId"]);
                if (!Page.IsPostBack)
                {
                    txtCreateOn.SelectedDate = DateTime.Now;
                    txtEnd.SelectedDate = Convert.ToDateTime(DateTime.Now).AddMonths(+3);
                    //CMSZonesBL.GetSub(dropZoneId, "0", 1, 0, CurrentUser.UserID);
                    CMSZonesBL.GetDataComboType(dropZoneId, "0", "(" + ModulType.SanPham.Type.ToString() + ")");//BinCombo Cateid                       
                    LoadData();
                }
            }
            catch (Exception ex)
            {
            }


        }
        #endregion

        #region LoadData
        private void LoadData()
        {
            try
            {
                if (productId > 0)
                {
                    btnUpdate.Visible = true;
                    btnAdd.Visible = false;
                    ProductsInfo info = ProductsBL.Select(productId);
                    if (info != null)
                    {
                        txtProductName.Text = info.ProductName;
                        txtContents.Text = info.Contents;
                        txtDescription.Text = info.Description;
                        dropPriority.SelectedValue = info.Priority.ToString();
                        txtPrice_VAT.Text = info.Price_VAT.ToString();
                        txtPrice.Text = info.Price.ToString();
                        txtKeyword.Text = info.Keyword;
                        dropZoneId.SelectedValue = info.ZoneId.ToString();
                        DropCity.SelectedValue = info.CityId.ToString();
                        SelectedCheckBox(info);
                        //txtAvatarSmail.Text = info.AvatarSmail;
                        //txtAvatarBig.Text = info.AvatarBig;
                        textImgAvatarBig.Text = info.AvatarBig;
                        textImgAvatarSmail.Text = info.AvatarSmail;
                        ImgAvatarSmail.ImageUrl = info.AvatarSmail;
                        ImgAvatarBig.ImageUrl = info.AvatarBig;
                        ImgAvatarBig.Visible = true;
                        ImgAvatarSmail.Visible = true;
                        txtDealXu.Text = info.DealXu.ToString();
                        txtEnd.SelectedDate = info.EndDate;
                        txtDescription_Hot.Text = info.Description_Hot;
                        txtDescription_Note.Text = info.Description_Note;
                    }
                }
                else
                {
                    btnUpdate.Visible = false;
                    btnAdd.Visible = true;
                    ImgAvatarSmail.Visible = false;
                    ImgAvatarBig.Visible = false;
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Có lỗi xảy ra Error: " + ex.Message;
            }
        }
        #endregion

        #region Load Data DropList

        void DropCityId()
        {
            //CityInfo cityInfo=new CityInfo();

            //DataTable tb = CityBL.SelectAll(cityInfo.CityId);
            //DropCity.DataSource = tb;

            //DropCity.DataTextField = "CityName";
            //DropCity.DataValueField = "CityId";
            //DropCity.DataBind();
            //DropCity.Items.Insert(0, new ListItem("Lựa chọn tên thành phố", "0"));
        }

        
       

        #endregion

        #region FillData
        void FillData(ProductsInfo info)
        {
       
            info.ProductId = productId;
            info.ProductName = txtProductName.Text;
            info.Description = txtDescription.Text;
            info.Price = float.Parse(txtPrice.Text);
            info.Contents = txtContents.Text;
            info.PageViews = 0;
            info.Keyword = txtKeyword.Text;
            info.CreatedOn = DateTime.Now;
            info.Status = Const.ChoDuyet;
            info.CreatedBy = CurrentUser.Username;
            info.CheckType = 0;
            info.CheckType = CheckType();
            info.ZoneId = Convert.ToInt32(dropZoneId.SelectedValue.ToString());
            info.Priority = Convert.ToInt32(dropPriority.SelectedValue.ToString());
            info.Price_VAT = float.Parse(txtPrice_VAT.Text);
            info.ModifiedBy =CurrentUser.Username;
            info.ModifiedOn = DateTime.Now;
            ImgAvatarSmail.ImageUrl = info.AvatarSmail;
            ImgAvatarBig.ImageUrl = info.AvatarBig;
            info.DealXu = Convert.ToInt32(txtDealXu.Text);
            info.Description_Hot = txtDescription_Hot.Text;
            info.Description_Note = txtDescription_Note.Text;
            info.EndDate = txtEnd.SelectedDate;

            if (txtAvatarSmail.Text.Trim().Length > 0)// Nếu thay đổi ảnh -> Thuc hien Crop anh
            {
                info.AvatarSmail = ConvertImage.SetAvatarThumbnailWidthFrame(Server.MapPath(txtAvatarSmail.Text.Trim()), 215,180, "_avatar");
            }
            else
            {
                info.AvatarSmail = textImgAvatarSmail.Text;
            }                           

            if (txtAvatarBig.Text.Trim().Length > 0)// Nếu thay đổi ảnh -> Thuc hien Crop anh
            {
                info.AvatarBig = ConvertImage.SetAvatarThumbnailWidthFrame(Server.MapPath(txtAvatarBig.Text.Trim()), 300,192, "_big");
                ConvertImage.SetAvatarThumbnailWidthFrame(Server.MapPath(txtAvatarBig.Text.Trim()), 476, 325, "_big_476x325");
            }
            else
            {
                info.AvatarBig = textImgAvatarBig.Text;
            }                      
        }
        #endregion

        #region thêm mới
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                ProductsInfo info = new ProductsInfo();
                FillData(info);
                int i = ProductsBL.Insert(info);
                if (i > 0)
                {
                    //lblUpdateStatus.Text = "Bạn đã thêm mới thành công";
                    //ClearText();
                    Response.Redirect("/cms/Default.aspx?ctrlkey=ListProductsWaiting");

                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }

        #endregion

        #region Sửa
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                ProductsInfo info = new ProductsInfo();
                FillData(info);
                ProductsBL.Update(info);
                lblUpdateStatus.Text = "Bạn đã chỉnh sửa thành công";
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        #region ClearText
        void ClearText()
        {
            txtProductName.Text = "";
            txtContents.Text = "";
            txtDescription.Text = "";
            txtPrice_VAT.Text = "";
            txtKeyword.Text = "";
        }

        #endregion

        #region CheckType
        int CheckType()
        {
            CheckBox[] Boxes = new CheckBox[4];
            Boxes[0] = chboxSanPhamNoiBat;
            Boxes[1] = chboxSanPhamHot;
            Boxes[2] = chboxSanPhamMoiNhat;
            Boxes[3] = chboxSanPhamGiamGia;
            return ClsCheckBox.ReturnStatusCheck(Boxes);
        }
        #endregion

        #region SelectedCheckBox
        void SelectedCheckBox(ProductsInfo productsInfo)
        {
            CheckBox[] Boxes = new CheckBox[4];
            Boxes[0] = chboxSanPhamNoiBat;
            Boxes[1] = chboxSanPhamHot;
            Boxes[2] = chboxSanPhamMoiNhat;
            Boxes[3] = chboxSanPhamGiamGia;

            chboxSanPhamNoiBat.Checked = ClsCheckBox.SelectChecked(ClsCheckBox.SanPhamNoiBat, productsInfo.CheckType, Boxes);
            chboxSanPhamHot.Checked = ClsCheckBox.SelectChecked(ClsCheckBox.SanPhamHot, productsInfo.CheckType, Boxes);
            chboxSanPhamMoiNhat.Checked = ClsCheckBox.SelectChecked(ClsCheckBox.SanPhamMoiNhat, productsInfo.CheckType, Boxes);
            chboxSanPhamGiamGia.Checked = ClsCheckBox.SelectChecked(ClsCheckBox.SanPhamGiamGia, productsInfo.CheckType, Boxes);
        }
        #endregion
       
    }
}